package com.scommunity.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scommunity.domain.Car;
import com.scommunity.domain.Parking;
import com.scommunity.mapper.CarMapper;
import com.scommunity.mapper.ParkingMapper;
import com.scommunity.service.IParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParkingServiceImpl extends ServiceImpl<ParkingMapper,Parking> implements IParkingService{
    @Autowired
    ParkingMapper parkingMapper;

    @Autowired
    CarMapper carMapper;
    @Override
    public PageInfo<Parking> all(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Parking> list = parkingMapper.selectList(null);
        PageInfo<Parking> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Parking add(Parking parking) {
        parkingMapper.insert(parking);
        return parkingMapper.selectOne(new QueryWrapper<Parking>().eq("parking_id",parking.getParkingId())
                .eq("parking_num",parking.getParkingNum()).eq("parking_price",parking.getParkingPrice())
                .eq("parking_Status",parking.getParkingStatus()).eq("parking_status",parking.getParkingStatus()));
    }

    @Override
    public void delete(Parking parking) {
        parkingMapper.deleteById(parking.getParkingId());
    }

    @Override
    public void updateN(Parking parking) {
        parkingMapper.updateById(parking);
    }

    @Override
    public PageInfo<Parking> getPageByCondition(Parking parking, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Parking> list = parkingMapper.getByCondition(parking);
        PageInfo<Parking> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Car findCarByOwnerId(Long ownerId) {
        return carMapper.selectOne(new QueryWrapper<Car>().eq("owner_id",ownerId));
    }

    @Override
    public List<Parking> getByCarId(Long carId) {
        return parkingMapper.selectList(new QueryWrapper<Parking>().eq("car_id",carId));
    }

}
